/**
 * 片头动画
 */
class openAnimation extends egret.DisplayObjectContainer {
    private imagess = Main.createBitmapByName("loading_png");

    private nowPage = 0;

    private time = 0;

    private miva = true;

    private shou = Main.createBitmapByName("6_1_png");
    private bgm = RES.getRes("bgm_mp3");

    public constructor() {
        super();
        this.once(egret.Event.ADDED_TO_STAGE, this.init, this);
    }

    private init() {
        this.addChild(this.imagess);
        let buffer = 0;
        const self = this;

        const txt = new egret.TextField();
        txt.text = "点击屏幕继续";
        this.stage.addChild(txt);
        Main.anchorCenter(txt);
        txt.textColor = 0;
        txt.x = this.stage.stageWidth * .5;
        txt.y = this.stage.stageHeight * .8;
        egret.Tween.get(txt, { loop: true })
            .to({ alpha: 0, }, 500)
            .wait(300)
            .to({ alpha: 1, }, 500);

        this.stage.addEventListener(egret.TouchEvent.TOUCH_BEGIN, touchStart, this);
        // this.stage.addEventListener(egret.TouchEvent.TOUCH_MOVE, miva, this);

        function touchStart(evt: egret.TouchEvent) {
            buffer = evt.stageX;
            if (this.nowPage >= 6) {
                this.dispatchEvent(new egret.Event("over"));
                this.stage.removeChild(this.shou);
                this.stage.removeEventListener(egret.TouchEvent.TOUCH_BEGIN, touchStart, this);
                this.stage.removeEventListener(egret.TouchEvent.TOUCH_MOVE, miva, this);
                return;
            }
            miva();

            if (self.miva) {
                self.stage.removeChild(txt);
                self.miva = false;
            }


        }

        function miva() {
            if (self.time > 0) return;
            // if (buffer - evt.stageX > 200) {
            if (self.nowPage == 0) {
                self.bgm.play();

            }

            if (self.nowPage == 5) {

                self.stage.addChild(self.shou);
                // Main.anchorCenter(shou);
                self.shou.anchorOffsetX = self.shou.width * .5;
                self.shou.x = self.stage.stageWidth * .5 + 100;
                self.shou.y = self.stage.stageHeight * .5 + 100;

                egret.Tween.get(self.shou, { loop: true })
                    .to({ alpha: 0, }, 500)
                    .wait(300)
                    .to({ alpha: 1, }, 500);
            }

            self.nowPage++;
            self.time++;
            self.imagess.texture = RES.getRes(`openAnimation_${self.nowPage}_png`);
            egret.Tween.get(self).wait(500).call(() => {
                self.time = 0;
            })
            // }
        }
    }
}